/**
 * Project Name:Bee
 * File Name:PayParswordPopupWindow.java
 * Package Name:com.longcai.p2p.bee.view
 * Date:2015-10-26下午1:08:05
 * Copyright (c) 2015,All Rights Reserved.
 */

package com.lc.user.express.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.lc.user.express.R;
import com.lc.user.express.app.AppContext;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @ClassName:PayParswordPopupWindow
 * @Function: TODO 输入支付密码
 * @Date: 2015-10-26 下午1:08:05
 * @since JDK 1.6
 */
public class PayParswordPopupWindow extends PopupWindow {

    private OnInputFinish onInputFinsh;

    private OnLeftClick onLeftClick;
    private OnRightClick onRightClick;

    private View mMenuView;

    private ImageView iv_turn;

    private LinearLayout ll_container, ll_container1;

    private ArrayList<String> mList = new ArrayList<String>();

    public PayParswordPopupWindow(final Activity context) {
        super(context);

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.layout_popup_window_pay_parsword, null);
        AppContext.ScaleScreenHelper.loadView((ViewGroup) mMenuView);

        iv_turn = (ImageView) mMenuView.findViewById(R.id.iv_turn);

        ll_container = (LinearLayout) mMenuView.findViewById(R.id.ll_container);
        ll_container1 = (LinearLayout) mMenuView.findViewById(R.id.ll_container1);

        this.setContentView(mMenuView);
        this.setWidth(LayoutParams.MATCH_PARENT);
        this.setHeight(LayoutParams.MATCH_PARENT);
        this.setFocusable(true);
        ColorDrawable dw = new ColorDrawable(0x80000000);
        this.setBackgroundDrawable(dw);
        mMenuView.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {

                int height = mMenuView.findViewById(R.id.pop_layout).getTop();
                int heightdown = mMenuView.findViewById(R.id.pop_layout).getBottom();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                    if (y > heightdown) {
                        dismiss();
                    }
                }
                return true;
            }
        });

        iv_turn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        ll_container1.addView(PayPasswordView.getInstance(context, new PayPasswordView.OnPayListener() {

            @Override
            public void onDelete() {

                // TODO Auto-generated method stub
                if (mList.size() > 0) {
                    ll_container.removeViewAt(mList.size() - 1);
                    mList.remove(mList.get(mList.size() - 1));
                }

            }

            @Override
            public void onInput(String password) {

                // TODO Auto-generated method stub
                if (mList.size() <= 0) {
                    addTextView(context, 1, password);
                    mList.add(password);
                } else if (mList.size() == 5) {
                    addTextView(context, 1, password);
                    mList.add(password);
                    onInputFinsh.onfinish(mList);
                    dismiss();
                } else if (mList.size() < 6) {
                    addTextView(context, 0, password);
                    mList.add(password);
                }

            }

        }).getView());

        ((TextView) mMenuView.findViewById(R.id.tv_change)).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onLeftClick != null) {
                    onLeftClick.onLeft();
                }
            }
        });
        ((TextView) mMenuView.findViewById(R.id.tv_forget)).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onRightClick != null) {
                    onRightClick.onRight();
                }
            }
        });
    }

    /**
     * @param context
     * @param type    0 两侧子view 1中间子view
     * @param s
     * @Title: addTextView
     * @Description: TODO
     * @author Administrator
     * @date: 2015-10-26 下午3:44:22
     */
    private void addTextView(Activity context, int type, String s) {
        LinearLayout ll = (LinearLayout) View.inflate(context, R.layout.item_key, null);
        if (type == 1) {
            ll.setLayoutParams(new LinearLayout.LayoutParams(AppContext.ScaleScreenHelper.getWidthHeight(116),// 两侧的view稍宽
                    LinearLayout.LayoutParams.WRAP_CONTENT));
        } else {
            ll.setLayoutParams(new LinearLayout.LayoutParams(AppContext.ScaleScreenHelper.getWidthHeight(110),
                    LinearLayout.LayoutParams.WRAP_CONTENT));
        }

        AppContext.ScaleScreenHelper.loadView(ll);

        TextView textView = (TextView) ll.findViewById(R.id.tv_key);
        textView.setText(s);

        ll_container.addView(ll);
    }

    public interface OnInputFinish {
        void onfinish(List<String> list);
    }

    public interface OnLeftClick {
        void onLeft();
    }

    public interface OnRightClick {
        void onRight();
    }

    public void setOnInputFinishListener(OnInputFinish onInputFinsh) {
        this.onInputFinsh = onInputFinsh;
    }

    public void setOnLeftClick(OnLeftClick onLeftClick) {
        this.onLeftClick = onLeftClick;
    }

    public void setOnRightClick(OnRightClick onRightClick) {
        this.onRightClick = onRightClick;
    }
}
